﻿using FrameworkCore.Events;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

namespace Application.HandleEvents.RestartEvents;

public partial class EventConsumer :
    IConsumer<Event<StopApplicationEvent>>
{
    private readonly IHostApplicationLifetime _appLifetime;
    private readonly ILogger<EventConsumer> _logger;

    public EventConsumer(
        IHostApplicationLifetime appLifetime,
        ILogger<EventConsumer> logger)
    {
        _appLifetime = appLifetime;
        _logger = logger;
    }

    public virtual Task HandleEventAsync(Event<StopApplicationEvent> eventMessage)
    {
        if (eventMessage.Entity.IsRestart)
        {
            _appLifetime.StopApplication();
            _logger.LogInformation("准备重启");
            return Task.CompletedTask;
        }

        _logger.LogInformation("无需重启");
        return Task.CompletedTask;
    }
}
